package com.bawei.permission.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bawei.permission.common.ResponseResult;
import com.bawei.permission.entity.User;
import com.bawei.permission.entity.UserRole;
import com.bawei.permission.entity.UserVo;
import com.bawei.permission.service.IUserRoleService;
import com.bawei.permission.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2020-06-03
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private IUserRoleService iUserRoleService;

    /**
     * 用户登录接口
     * @param user
     * @return
     */
    @PostMapping("login")
    public ResponseResult login(@RequestBody User user){
        //验证用户名和密码是否为空
        if(StringUtils.isBlank(user.getPassword()) || StringUtils.isBlank(user.getPassword())){
            return ResponseResult.error(10000,"用户名或密码不能为空");
        }
        //查询一条用户信息
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",user.getUsername());
        User userInfo = iUserService.getOne(queryWrapper);
        //用户是否停用
        if(!userInfo.getEnabled()){
            return ResponseResult.error(10000,"用户名或密码不能为空");
        }
        //验证密码
        if(userInfo==null || !userInfo.getPassword().equals(user.getPassword())){
            return ResponseResult.error(10000,"用户名或密码不能为空");
        }
        //设置密码为空
        userInfo.setPassword(null);
        return ResponseResult.success(userInfo);
    }

    /**
     * 用户分页列表接口
     * @param userVo
     * @param page
     * @return
     */
    @GetMapping("list")
    public ResponseResult list(UserVo userVo, Page page){
        IPage<UserVo> pageInfo = iUserService.getPageInfo(userVo,page);
        return ResponseResult.success(pageInfo);
    }

    /**
     * 保存（新增或修改）
     * @param user
     * @return
     */
    @PostMapping("save")
    public ResponseResult save(@RequestBody  User user){
        //创建用户时，设置默认密码
        if(user.getId()==null){
            user.setPassword("123456");
        }
        //保存用户信息
        boolean b = iUserService.saveOrUpdate(user);
        return ResponseResult.success();
    }

    /**
     *  查询用户关联的角色Id
     * @return
     */
    @GetMapping("getRoleIdListByUserId")
    public ResponseResult getRoleIdListByUserId(String userId){
        //查询用户角色
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uid",userId);
        List<UserRole> list = iUserRoleService.list(queryWrapper);
        //返回角色Id
        List<Integer> roleIdList = list.stream().map(ur->ur.getRid()).collect(Collectors.toList());
        return ResponseResult.success(roleIdList);
    }

    /**
     * 保存用户角色
     * @param userVo
     * @return
     */
    @PostMapping("bindRoles")
    public ResponseResult bindRoles(@RequestBody UserVo userVo){
        //删除角色
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uid",userVo.getId());
        iUserRoleService.remove(queryWrapper);
        //添加用户角色
        userVo.getRoleIdList().forEach(roleId->{
            UserRole userRole = new UserRole();
            userRole.setUid(userVo.getId());
            userRole.setRid(roleId);
            iUserRoleService.save(userRole);
        });
        return ResponseResult.success();
    }

}
